<?php
/*
Template Name: Assistiti
*/
?>

<?php get_header(); ?>

<?php 

global $blogurl;

?>


<script>

$(document).ready(function() {

	$(document).on('pageinit', '#assistiti', function(){      
	    //var url = 'http://195.128.234.70:8080/webservice/WelcomeServlet?idoperatore=5&nuovo=true';
	
		var url = '<?php echo get_remote_url($rem_url, $rem_port, "WelcomeServlet", $rem_id); ?>&nuovo=true';
	              
	    
	    $.ajax({
	        url: url ,
	        dataType: "json",
	        async: true,
	        success: function (result) {
				
	            ajax.parseJSONP(result);
	        },
	        error: function (request,error) {
	            alert('Network error has occurred please try again!');
	        }
	    });         
	});
	
	$(document).on('pagebeforeshow', '#headline', function(){    
	
	//var url = 'http://195.128.234.70:8080/webservice/AssistitiAnaServlet?idassistito='+ movieInfo.id ;

	var url = '<?php echo get_remote_url($rem_url, $rem_port, "AssistitiAnaServlet", $rem_id); ?>&idassistito' + movieInfo.id;
	              
	    $('#titoloAnagrafica').empty();
	    $.ajax({
	        url: url ,
	        dataType: "json",
	        async: true,
	        success: function (result) {
				
	            $('#titoloAnagrafica').append('<li>Assistito :  '+ result.users[0].cognome +' '+result.users[0].nome+'</li>');
				$('#titoloAnagrafica').append('<li>Nato il :  '+  result.users[0].datan +'</li>');
				$('#indirizzo-a').val(result.users[0].indirizzo);
				$('#comune-a').val(result.users[0].comune);
				$('#tel-1').val(result.users[0].tel1);
				$('#tel-2').val(result.users[0].tel2);
				$('#cel-1').val(result.users[0].cel1);
				$('#cel-2').val(result.users[0].cel2);
				
				
				
	        },
	        error: function (request,error) {
	            alert('Network error has occurred please try again!');
	        }
	    });  
	    
	
	  
	});
	
	$(document).on('vclick', '#movie-list li a', function(){  
	    movieInfo.id = $(this).attr('data-id');
		
	    $.mobile.changePage( "#headline", { transition: "slide", changeHash: false });
	});
	
	$(document).on('vclick', '#submit-a', function(){  
	
	//var url = 'http://195.128.234.70:8080/webservice/AssistitiAnaSaveServlet?idassistito='+movieInfo.id+'&tel1='+$('#tel-1').val()+'&tel2='+$('#tel-2').val()+'&cel1='+$('#cel-1').val()+'&cel2='+$('#cel-2').val();

	var url = '<?php echo get_remote_url($rem_url, $rem_port, "AssistitiAnaServlet", $rem_id); ?>&idassistito' + movieInfo.id+'&tel1='+$('#tel-1').val()+'&tel2='+$('#tel-2').val()+'&cel1='+$('#cel-1').val()+'&cel2='+$('#cel-2').val();             
	    
	    $.ajax({
	        url: url ,
	        dataType: "json",
	        async: true,
	        success: function (result) {
				
	           
			$( "#popupDialog" ).popup("open");
				
	        },
	        error: function (request,error) {
	            alert('Network error has occurred please try again!');
	        }
	    });  
	});
	
	var movieInfo = {
	    id : null,
	    result : null
	}
	
	var ajax = {  
	    parseJSONP:function(result){  
	        movieInfo.result = result.users;
	        $.each(result.users, function(i, row) {
	           // console.log(JSON.stringify(row));
	            $('#movie-list').append('<li><a href="" data-id="' + row.mobile + '"><h3>' + row.cognome +' '+row.nome+ '</h3><p>' + row.datan + '</p></a></li>');
	        });
	        $('#movie-list').listview('refresh');
	    }
	}
});
</script>


	<div role="main" class="ui-content jqm-content">

		<?php  

		if (current_user_can('view_assisted')) {		
	    	if (have_posts()) {
	    		while (have_posts()) {
	    			the_post();	    		
	    			?>
	    			<h1><?php the_title(); ?></h1>
	    			
	    			<?php 
					if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { 
						the_post_thumbnail('full', array("class" => "img-thumbnail"));
					} 
					?>	
	    			
	    			<?php the_content(); ?>    			
		
						<form class="ui-filterable">
					       <input id="filterBasic-input" data-type="search">
					    </form>
				        
				        <ul data-role="listview"  data-filter="true" data-input="#filterBasic-input" id="movie-list" data-theme="a">
				        
				        </ul>
	    			
	    		<?php 
	    		}
	    	}     	
		} else {		
			echo no_permissions(); 		
		}
    	
    	?>


	</div><!-- /content -->
	
<?php require_once ( get_template_directory() . '/menu.php' );?>
  
<?php get_sidebar(); ?>

<?php get_footer(); ?>



<?php if (current_user_can('view_assisted')) { ?>

	<div data-role="page" id="headline">
	    <div data-theme="a" data-role="header">
	        <a href="<?php echo $blogurl; ?>/assistiti/#assistiti" class="ui-btn-left" data-transition="slide" data-direction="reverse">Back</a>                        
	        <h3>
	            <div id="titoloAnagrafica"></div>
	        </h3>
	    </div>        
	    <div data-role="content">
	        
	        <div class="ui-field-contain">
				<label for="indirizzo-a">Indirizzo:</label>
				<input type="text" name="indirizzo-a" id="indirizzo-a" placeholder="Indirizzo" value="">
			</div>  
			<div class="ui-field-contain">
				<label for="comune-a">Comune:</label>
				<input type="text" name="comune-a" id="comune-a" placeholder="Comune" value="">
			</div>  
			<div class="ui-field-contain">
				<label for="tel-1">Telefono 1:</label>
				<input type="text" name="tel-1" id="tel-1" placeholder="Telefono 1" value="">
			</div> 
			<div class="ui-field-contain">
				<label for="tel-2">Telefono 2:</label>
				<input type="text" name="tel-2" id="tel-2" placeholder="Telefono 2" value="">
			</div> 
			<div class="ui-field-contain">
				<label for="cel-1">Cellulare 1:</label>
				<input type="text" name="cel-1" id="cel-1" placeholder="Cellulare 1" value="">
			</div> 
			<div class="ui-field-contain">
				<label for="cel-2">Cellulare 2:</label>
				<input type="text" name="cel-2" id="cel-2" placeholder="Cellulare 2" value="">
			</div>  
			<button type="submit" id="submit-a" class="ui-shadow ui-btn ui-corner-all">Salva</button>
	        </ul>
			<div data-role="popup" id="popupDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
			    <div data-role="header" data-theme="a">
			    
			    	<h1>Salvataggio avvenuto</h1>
			    
			    </div>
			    <div role="main" class="ui-content">
			        <h3 class="ui-title">Le informazioni sono state aggiornate</h3>
			    
			        <a  class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b" data-rel="back">OK</a>
			       
			    </div>
			</div>
	    </div>
	</div>  
	
<?php 
} else {		
	echo no_permissions(); 		
}
?>